package gui.admin;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import objects.DistanceLog;
import objects.GeneralLog;
import objects.SpeedLogReport;
import objects.StopSignLog;
import objects.Student;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

import analysis.DistanceRecord;
import analysis.Record;
import analysis.Reports;
import analysis.SeatbeltRecord;
import analysis.SpeedRecord;
import analysis.StopSignRecord;




public class AdminDriverAnalysisPanelImpl extends AbstractAdminPanelImpl implements AdminDriverAnalysisPanel {
	private Tree table;
	private Label nameLbl;
	private Label datesLbl;
	private TreeColumn dateCol;
	private TreeColumn eventTypeCol;
	private TreeColumn durationCol;
	private TreeColumn pointsCol;
	private TreeColumn weightCol;
	private Label name;
	private Button returnBtn;
	
	private List<TreeItem> store = new ArrayList<TreeItem>();
	private TreeColumn tblclmnFrequency;
	private Label drivingPointsLbl;
	private Button loadBtn;
	private Label pointsPerHourLbl;
	private Label totalDrivingTimeLbl;
	private Label drivingPoints;
	private Label pointsPerHour;
	private Label totalTime;
	private Label dates;
	
	private Student student;
	private Reports reports = new Reports();
	private ArrayList<Record> records;
	
	public AdminDriverAnalysisPanelImpl(Display display, Shell shell) {
		super(display, shell);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	public void open() {
		// Remove at compile time
//		Shell shell = new Shell();
		// **********************
		
		shell.setSize(800, 600);
		shell.setText("SWT Application");
		
		table = new Tree(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 116, 764, 379);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		dateCol = new TreeColumn(table, SWT.NONE);
		dateCol.setWidth(133);
		dateCol.setText("Date");
		
		eventTypeCol = new TreeColumn(table, SWT.NONE);
		eventTypeCol.setWidth(183);
		eventTypeCol.setText("Event Type");
		
		durationCol = new TreeColumn(table, SWT.NONE);
		durationCol.setWidth(90);
		durationCol.setText("Duration (sec)");
		
		tblclmnFrequency = new TreeColumn(table, SWT.NONE);
		tblclmnFrequency.setWidth(100);
		tblclmnFrequency.setText("Frequency");
		
		weightCol = new TreeColumn(table, SWT.NONE);
		weightCol.setWidth(90);
		weightCol.setText("Weight");
		
		pointsCol = new TreeColumn(table, SWT.NONE);
		pointsCol.setWidth(147);
		pointsCol.setText("Driver Points");
		
		nameLbl = new Label(shell, SWT.NONE);
		nameLbl.setBounds(10, 29, 60, 24);
		nameLbl.setText("Name:");
		nameLbl.setFont(new Font(display, "Arial", 16, SWT.NONE));
		
		datesLbl = new Label(shell, SWT.NONE);
		datesLbl.setBounds(10, 59, 62, 21);
		datesLbl.setText("Dates:");
		datesLbl.setFont(new Font(display, "Arial", 16, SWT.NONE));
		
		name = new Label(shell, SWT.NONE);
		name.setBounds(76, 29, 252, 24);
		name.setFont(new Font(display, "Arial", 16, SWT.NONE));
		
		returnBtn = new Button(shell, SWT.NONE);
		returnBtn.setBounds(699, 501, 75, 25);
		returnBtn.setText("Return");
		returnBtn.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				AdminDriverProfilePanel window = new AdminDriverProfilePanelImpl(display, shell);
				window.setStudent(student);
				dispose();
				window.open();
			}
		});
		
		drivingPointsLbl = new Label(shell, SWT.NONE);
		drivingPointsLbl.setBounds(334, 29, 186, 24);
		drivingPointsLbl.setText("Total Driving Points:");
		drivingPointsLbl.setFont(new Font(display, "Arial", 16, SWT.NONE));
		
		pointsPerHourLbl = new Label(shell, SWT.NONE);
		pointsPerHourLbl.setBounds(334, 59, 154, 21);
		pointsPerHourLbl.setText("Points Per Hour:");
		pointsPerHourLbl.setFont(new Font(display, "Arial", 16, SWT.NONE));
		
		totalDrivingTimeLbl = new Label(shell, SWT.NONE);
		totalDrivingTimeLbl.setBounds(334, 86, 171, 24);
		totalDrivingTimeLbl.setText("Total Driving Time:");
		totalDrivingTimeLbl.setFont(new Font(display, "Arial", 16, SWT.NONE));
		
		loadBtn = new Button(shell, SWT.NONE);
		loadBtn.setBounds(10, 501, 75, 25);
		loadBtn.setText("Load");
		loadBtn.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				
			}
		});
		
		drivingPoints = new Label(shell, SWT.NONE);
		drivingPoints.setBounds(526, 29, 89, 24);
		drivingPoints.setFont(new Font(display, "Arial", 16, SWT.NONE));
		
		pointsPerHour = new Label(shell, SWT.NONE);
		pointsPerHour.setBounds(495, 59, 89, 24);
		pointsPerHour.setFont(new Font(display, "Arial", 16, SWT.NONE));
		
		totalTime = new Label(shell, SWT.NONE);
		totalTime.setBounds(511, 86, 89, 24);
		totalTime.setFont(new Font(display, "Arial", 16, SWT.NONE));
		
		dates = new Label(shell, SWT.NONE);
		dates.setBounds(78, 60, 250, 21);
		dates.setFont(new Font(display, "Arial", 14, SWT.NONE));
		
		// Generate Report
		reports.generateReport(student.getID());
		fillReport();
		
		// Set Labels
		setName(student.getName());
		
		SimpleDateFormat fmt = new SimpleDateFormat("MMM d, yyyy");
		Date d = new Date(records.get(0).getDate());
		String date = fmt.format(d) + " to ";
		d = new Date(records.get(records.size()-1).getDate());
		date += fmt.format(d);
		setDates(date);
		
		DecimalFormat dfmt = new DecimalFormat("#.##");
		setPointsPerHour(dfmt.format(reports.getPtsPerHour()));
		setDrivingPoints(dfmt.format(reports.getTotDriverPts()));
		setTotalTime(Double.toString(student.getHours()));

		shell.open();
		shell.layout();
	}

	public void addEvent(String[] event) {
		if(event.length == 6) {
			TreeItem item = new TreeItem(table, SWT.NONE);
			item.setText(event);
			store.add(item);
		}
	}
	
	public void addSubEvent(String[] event, int index) {
		if(event.length == 6) {
			TreeItem item = new TreeItem(store.get(index), SWT.NONE);
			item.setText(event);
//			store.add(item);
		}
	}

	public void setName(String name) {
		this.name.setText(name);
		this.name.update();
	}

	public void setDates(String dates) {
		this.dates.setText(dates);
		this.dates.update();
	}
	
	public void setDrivingPoints(String drivingPoints) {
		this.drivingPoints.setText(drivingPoints);
		this.drivingPoints.update();
	}
	
	public void setPointsPerHour(String pointsPerHour) {
		this.pointsPerHour.setText(pointsPerHour);
		this.pointsPerHour.update();
	}
	
	public void setTotalTime(String totalTime) {
		this.totalTime.setText(totalTime + " hrs");
		this.totalTime.update();
	}
	
	public void setStudent(Student student) {
		this.student = student;
	}
	
	@SuppressWarnings("unused")
	public void fillReport() {
		records = reports.getRecords();
		DecimalFormat dfmt = new DecimalFormat("#.##");
		int index = 0;
		for(Record r : records) {
			SimpleDateFormat fmt = new SimpleDateFormat("MMM d, yyyy");
			Date d = new Date(r.getDate());
			String date = fmt.format(d);
			addEvent(new String[] {
					date,
					r.getType(),
					Integer.toString(r.getDuration()/1000),
					Integer.toString(r.getFrequency()),
					dfmt.format(r.getWeight()),
					dfmt.format(r.getPoints())
				});
			
			if(r instanceof SpeedRecord) {
				for(SpeedLogReport s : ((SpeedRecord)r).getSpeedlogs()) {
					addSubEvent(new String[] {
							date,
							r.getType(),
							Integer.toString(s.getDuration()/1000),
							"1",
							dfmt.format(r.getWeight()),
							""
					}, index);
				}
			} else if(r instanceof SeatbeltRecord) {
				for(GeneralLog g : ((SeatbeltRecord)r).getSeatbeltlogs()) {
					addSubEvent(new String[] {
						date,
						r.getType(),
						Integer.toString(g.getDuration()/1000),
						"1",
						dfmt.format(r.getWeight()),
						""
					}, index);
				}
			} else if(r instanceof StopSignRecord) {
				for(StopSignLog s : ((StopSignRecord)r).getStoplogs()) {
					addSubEvent(new String[] {
							date,
							r.getType(),
							"",
							"1",
							dfmt.format(r.getWeight()),
							""
					}, index);
				}
			} else if(r instanceof DistanceRecord) {
				for(DistanceLog l : ((DistanceRecord)r).getDistancelogs()) {
					addSubEvent(new String[] {
							date,
							r.getType(),
							Integer.toString(l.getDuration()/1000),
							"1",
							dfmt.format(r.getWeight()),
							""
					}, index);
				}
			}
			
			index++;
		}
		
	}
}
